home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / tools / cedit.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  296 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    cedit - 
  19.  *        A simple color editor. Use the left mouse button to 
  20.  *    pick the color on the screen to edit, then move the sliders to 
  21.  *    change the color.
  22.  *
  23.  *                Paul Haeberli - 1984
  24.  *
  25.  */
  26. #include <stdio.h>
  27. #include <gl/gl.h>
  28. #include <gl/device.h>
  29. #include "port.h"
  30.  
  31. #define DISPSIZE    150
  32.  
  33. float or = -1.0;
  34. float og = -1.0;
  35. float ob = -1.0;
  36. int cc = -1;
  37. float cr, cg, cb;
  38. int m1, m2, m3;
  39. long xorg, yorg;
  40. int x, y;
  41. long xsize, ysize;
  42. float wx, wy;
  43. int menu;
  44. int togglehex = 0;
  45.  
  46. main(argc,argv)
  47. int argc;
  48. char **argv;
  49. {
  50.     int i, j;
  51.  
  52.     if (argc>1)
  53.     setcolorsys(atoi(argv[1]));
  54.     keepaspect(1,1);
  55.     glcompat(GLC_SOFTATTACH, TRUE);
  56.     winopen("cedit");
  57.     menu = defpup("colorsys %t|rgb|cmy|hsv|hls|toggle numbers");
  58.     initmouse();
  59.     if(getplanes()<=9)    /* greybase hardcoded to 512, so no good if less */
  60.         cc = 64;
  61.     else
  62.     cc = 576;    /* what it used to be */
  63.     makeframe();
  64.     while (1) {
  65.     checkmouse();
  66.     if (m3) {
  67.         x = getvaluator(MOUSEX);
  68.         y = getvaluator(MOUSEY);
  69.         wx = 10.0*(x-xorg)/xsize;
  70.         wy = 10.0*(y-yorg)/ysize;
  71.         if (wy>0.0 && wy<10.0) {
  72.         if (wx>1.0 && wx<2.0) {
  73.              cr = (wy-2.0)/7.0;
  74.              drawsliders(cc,cr,cg,cb);
  75.         } else if (wx>3.0 && wx<4.0) {
  76.              cg = (wy-2.0)/7.0;
  77.              drawsliders(cc,cr,cg,cb);
  78.         } else if (wx>5.0 && wx<6.0) {
  79.              cb = (wy-2.0)/7.0;
  80.              drawsliders(cc,cr,cg,cb);
  81.         }
  82.         } 
  83.     }
  84.     }
  85. }
  86.  
  87. newcolor(c)
  88. int c;
  89. {
  90.     int savetoggle;
  91.  
  92.     cc = c;
  93.     color(c);
  94.     rectf(7.0,2.0,9.0,9.0);
  95.     grey(0.0);
  96.     rect(7.0,2.0,9.0,9.0);
  97.     modgetmcolor(c,&cr,&cg,&cb);
  98.     or = og = ob = -1.0;
  99.     drawback();
  100.     drawsliders(cc,cr,cg,cb);
  101.     if(xsize>DISPSIZE) {
  102.     savetoggle = togglehex;
  103.     togglehex = 0;
  104.     centernum(cc,8.0,0.3);
  105.     togglehex = savetoggle;
  106.     }
  107. }
  108.  
  109. drawsliders(c,r,g,b)
  110. int c;
  111. float r, g, b;
  112. {
  113.     int changed;
  114.  
  115.     changed = 0;
  116.     if (r<0.0) r = 0.0;
  117.     if (r>1.0) r = 1.0;
  118.     if (r != or) {
  119.     drawknob(0.0,or,r,rgb(1.0,0.0,0.0));
  120.         or = r;
  121.     if(xsize>DISPSIZE) 
  122.         centernum((int)(r*255),1.5,0.3);
  123.     changed++;
  124.     }
  125.     if (g<0.0) g = 0.0;
  126.     if (g>1.0) g = 1.0;
  127.     if (g != og) {
  128.     drawknob(2.0,og,g,rgb(0.0,1.0,0.0));
  129.         og = g;
  130.     if(xsize>DISPSIZE) 
  131.         centernum((int)(g*255),3.5,0.3);
  132.     changed++;
  133.     }
  134.     if (b<0.0) b = 0.0;
  135.     if (b>1.0) b = 1.0;
  136.     if (b != ob) {
  137.     drawknob(4.0,ob,b,rgb(0.0,0.0,1.0));
  138.         ob = b;
  139.     if(xsize>DISPSIZE) 
  140.         centernum((int)(b*255),5.5,0.3);
  141.     changed++;
  142.     }
  143.     if (changed) 
  144.     modmapcolor(c,r,g,b);
  145. }
  146.  
  147. drawknob(x,old,new,c)
  148. float x, old, new;
  149. int c;
  150. {
  151.      pushmatrix();
  152.      translate(x,2.0+(7.0*old),0.0);
  153.      grey(0.9);
  154.      rectf(1.2,-0.10,1.8,0.10);    
  155.      popmatrix();
  156.      pushmatrix();
  157.      translate(x,2.0+(7.0*new),0.0);
  158.      color(c);
  159.      rectf(1.2,-0.10,1.8,0.10);    
  160.      popmatrix();
  161. }
  162.  
  163. makeback(x)
  164. float x;
  165. {
  166.     pushmatrix();
  167.         translate(x,0.0,0.0);
  168.     grey(0.9);
  169.     rectf(1.0,1.6,2.0,9.4);
  170.     grey(0.0);
  171.     rect(1.0,1.6,2.0,9.4);
  172.     popmatrix();
  173. }
  174.  
  175. drawback()
  176. {
  177.     makeback(0.0);
  178.     makeback(2.0);
  179.     makeback(4.0);
  180. }
  181.  
  182. initmouse()
  183. {
  184.     qdevice(MENUBUTTON);
  185.     qdevice(MIDDLEMOUSE);
  186.     qdevice(LEFTMOUSE);
  187. }
  188.  
  189. checkmouse()
  190. {
  191.     short dev, val;
  192.     int sel;
  193.  
  194.     while (1) {
  195.     if (m1 != 0 || m2 != 0 || m3 != 0) {
  196.         if (!qtest()) 
  197.         return;
  198.     } 
  199.     dev = qread(&val);
  200.     switch (dev) {
  201.         case MENUBUTTON: 
  202.             sel = dopup(menu);    
  203.             if (sel>0) {
  204.                 if (sel == 5)
  205.                 togglehex = (togglehex == 1) ? 0 : 1;
  206.                 else 
  207.                 setcolorsys(sel);
  208.                 newcolor(cc);
  209.             }
  210.             break;
  211.         case MIDDLEMOUSE: 
  212.             m2 = val;
  213.             if (m2 == 0) {
  214.                 x = getvaluator(MOUSEX);
  215.                 y = getvaluator(MOUSEY);
  216.                 wx = 10.0*(x-xorg)/xsize;
  217.                 wy = 10.0*(y-yorg)/ysize;
  218.                 if (wx<-0.5 || wx>10.5 || wy<-0.5 || wy>10.5)
  219.                 modmapcolor(getapixel(x,y),cr,cg,cb);
  220.             }
  221.             break;
  222.         case LEFTMOUSE: 
  223.             m3 = val;
  224.             if (m3 == 0) {
  225.                 x = getvaluator(MOUSEX);
  226.                 y = getvaluator(MOUSEY);
  227.                 wx = 10.0*(x-xorg)/xsize;
  228.                 wy = 10.0*(y-yorg)/ysize;
  229.                 if (wx<-0.5 || wx>10.5 || wy<-0.5 || wy>10.5)
  230.                 newcolor(getapixel(x,y));
  231.             }
  232.             break;
  233.         case REDRAW: 
  234.             makeframe();
  235.             break;
  236.     }
  237.     }
  238. }
  239.  
  240. makeframe()
  241. {
  242.     reshapeviewport();
  243.     getorigin(&xorg,&yorg);
  244.     getsize(&xsize,&ysize);
  245.     grey(0.8);
  246.     clear();
  247.     ortho2(0.0,10.0,0.0,10.0);
  248.     grey(0.0);
  249.     newcolor(cc);
  250. }
  251.  
  252. modmapcolor(c,r,g,b)
  253. int c;
  254. float r, g, b;
  255. {
  256.     float fr, fg, fb;
  257.     int ir, ig, ib;
  258.  
  259.     torgb(r,g,b,&fr,&fg,&fb);
  260.     rgb_to_irgb(fr,fg,fb,&ir,&ig,&ib);
  261.     gammapcolor(c,ir,ig,ib);
  262. }
  263.  
  264. modgetmcolor(c,r,g,b)
  265. int c;
  266. float *r, *g, *b;
  267. {
  268.     unsigned short cr, cg, cb;
  269.     float fr, fg, fb;
  270.  
  271.     gamgetmcolor(c,&cr,&cg,&cb);
  272.     irgb_to_rgb(cr,cg,cb,&fr,&fg,&fb);
  273.     fromrgb(fr,fg,fb,r,g,b);
  274. }
  275.  
  276. centernum(val,x,y)
  277. int val;
  278. float x, y;
  279. {
  280.     char buf[128];
  281.     int pixlen;
  282.     float flen;
  283.  
  284.     grey(0.8);
  285.     rectf(x-1.0,y-0.3,x+1.0,y+1.2);
  286.     grey(0.0);
  287.     if (togglehex)
  288.         sprintf(buf,"0x%02x",val);
  289.     else
  290.         sprintf(buf,"%d",val);
  291.     pixlen = strwidth(buf);
  292.     flen = (pixlen*10.0)/xsize;
  293.     cmov2(x-flen/2.0,y);
  294.     charstr(buf);
  295. }
  296.